/*
 * Amkai Enterprise Solution (AES.) 
 * Copyright 2004-2008 by Amkai(TM), Inc.
 * www.amkai.com
 * 
 * Project: webservice
 * Package: com.amkai.aes.webservice.binary.objecthandler.inventory
 * File: RADBeanQueryHandler.java
 * Created: Apr 30, 2008 1:26:59 PM
 * Author: kokog 
 * 
 */
package com.amkai.aes.webservice.binary.objecthandler.inventory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.amkai.aes.aescommon.service.binary.job.inventory.RADBeanQueryJob;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;
import com.amkai.rad.designerbase.beans.RADBean;

public class RADBeanQueryHandler implements JobObjectHandler<RADBeanQueryJob, Serializable, ArrayList<RADBean>> {

	@Override
	public ArrayList<RADBean> process(RADBeanQueryJob job, Serializable object) throws Exception {
		SessionManager sessionManager = SiteManager.Start();
		sessionManager.openSession();
		String q = "from "+job.getBeanClass().getCanonicalName();
		String[] orderByFieldNames = job.getOrderByFieldNames();
		if (orderByFieldNames!=null && orderByFieldNames.length>0) {
			q += " as x order by";
			boolean started = false;
			for (String n : orderByFieldNames) {
				q += " x." + n;
				if (started)
					q += ",";
				else
					started = true;
			}
		}
		List<RADBean> list = sessionManager.execute(q).list();
		ArrayList<RADBean> ret = new ArrayList<RADBean>(list);
		return ret;
	}

}
